// Copyright 2013 UC Mobile Ltd.

/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.

 * You may obtain a copy of the license at
 * http://www.gnu.org/licenses/
 */


/**
 * @author
 * Pablo Han, handong@ucweb.com
 * Scott He, hejt@ucweb.com
 *
 * @fileoverview
 * This library is designed to distinguish UC Browser and non-UC Browser
 * for different displaying.
 */

function getattr(str,name)
{
var start=str.indexOf(name+"=");
var sub=(start==-1)?"":str.substring(start+name.length+1);
var stop=sub.indexOf('&');
var sub=(stop==-1)?sub:sub.substring(0,stop);
return(sub);
}

if(window.fishhook){fishhook=fishhook+1;}
else{var fishhook=1;}

var attr = document.getElementsByClassName("fish-mouth")[fishhook-1];
var src = attr.src;
var pub = getattr(src,"pub");

var lnglist = "es,ru,id,pt,ru,vi";
var lng = "en";
var language = navigator.language?navigator.language.split(","):navigator.browserLanguage.split(",");
for (x in language){
  var lngcode = language[x].toLowerCase().substring(0,2);
    if (lnglist.indexOf(lngcode)!=-1){
      lng=lngcode;
      break;
    }
}

var width = document.documentElement.clientWidth;
if(!width){width = document.body.clientWidth;}
if(!width){width = window.screen.width/2;}

var height = document.documentElement.clientHeight;
if(!height){height = document.body.clientHeight;}
if(!height){height = window.screen.height/2;}

var max; 
var type;
var promotion;
var sentence;

switch (lng){
case "en":
max=5;
type=".png";
promotion="Download UC Browser!";
break;
case "es":
max=4;
type=".png"; 
promotion="Descarga UC Browser!";
break;
case "id":
max=3;
type=".jpg";
promotion="Ambil UC Browser!";
break;
case "pt":
max=3;
type=".png";
promotion="Baixar UC Browser!";
break;
case "ru":
max=3;
type=".png";
promotion="Скачать UC Browser!";
break;
case "vi":
max=3;
type=".jpg";
promotion="Tải về UC Browser!";
}

var num="0"+Math.floor((Math.random()*max)+1);

if(width>=320&&height>=50){width=320;height=50;}
else if(width>=300&&height>=50){width=300;height=50;}
else if(width>=216&&height>=36){width=216;height=36;}
else if(width>=168&&height>=28){width=168;height=28;}
else if(width>=120&&height>=20){width=120;height=20;}
else sentence=promotion;

if (!sentence) {
sentence="<img src='http://union.ucweb.com/images/"+lng+"/"+width+"x"+height+"/"+num+type+"' width='"+width+"' height='"+height+"' alt='"+promotion+"'/>";
}

if (navigator.userAgent.indexOf("UCBrowser") != -1 || navigator.userAgent.indexOf("1.2.3.4") != -1) {
document.write("");
}
else {
document.write("<a href='http://down3.ucweb.com/ucbrowser/en/?bid=444&amp;pub="+pub+"'>"+sentence+"</a>");
}
